package com.chuangyiruanke.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import com.chuangyiruanke.util.BitArray;

public class DotMatrixFont {

    private int bytePerChar;
    private int width;
    private int height;
    private byte[] values;

    public int getBytePerChar() {
        return bytePerChar;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public DotMatrixFont(InputStream stream, int width, int height) throws IOException {
        this.width = width;
        this.height = height;
        bytePerChar = (width * height) >> 3;
        read(stream);
    }

    public DotMatrixFont(String fontFileName, int width, int height) throws IOException {
        this(new FileInputStream(fontFileName), width, height);
    }

    private void read(InputStream stream) throws IOException {
        stream.reset();
        values = new byte[stream.available()];
        stream.read(values, 0, values.length);
    }

    public BitArray getMatrix(String str) {
        return null;
    }

    public byte[] getAsciiMatrix(byte code) {
        byte[] result = new byte[bytePerChar];
        int start = 0;
        int character = code & 0xff;
        System.arraycopy(values, character * bytePerChar, result, start, bytePerChar);
        return result;
    }

    public byte[] getGBKMatrix(byte[] code) {
        byte[] result = new byte[bytePerChar * code.length / 2];
        int start = 0;
        for (int i = 0; i < code.length; i += 2) {
            int area = (code[i] & 0xff) - 0xa0;
            int pos = (code[i + 1] & 0xff) - 0xa0;
            int offset = bytePerChar * ((area - 1) * 94 + pos - 1);
            System.arraycopy(values, offset, result, start, bytePerChar);
            start += bytePerChar;
        }
        return result;
    }

}
